package com.jack.thinking.java._22.swing.base;

import java.awt.HeadlessException;
import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class SafeHelloSwing extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JLabel label;

	public SafeHelloSwing() throws HeadlessException {
		super("Hello Swing");
		label = new JLabel("A Label");
		add(label);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 300);
		setVisible(true);
	}
	
	static SafeHelloSwing shs;
	
	public static void main(String[] args) throws Exception {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				shs = new SafeHelloSwing();
			}
		});
		
		/**
		 * 请注意： sleep() 调用如果放到了构造器内部的话， 就会中止 事件分发线程， 然后后面的事件分发可能顶掉之前的 事件， 通常不要怎么做
		 */
		TimeUnit.SECONDS.sleep(1);
		
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				shs.label.setText("Hey ! Girl");
			}
		});
	}
}
